<!--这是产品分类组件-->
<template>
	<div class="app-container">
    <div class="filter-container c-flex-row c-aligni-center c-justify-sb">
      <el-form class="form-container">
        <el-input @keyup.enter.native="handleFilter" v-model="listQuery.nameInput" clearable placeholder="搜索名称" style="width: 250px;"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      </el-form>
			<div v-if="permission == 3">
        <el-button type="primary" icon="el-icon-edit" @click="addTag(-1)">新增</el-button>
      </div>
		</div>
		<div>
			<el-table :data="tableData" row-key="id" element-loading-text="加载中..." :header-row-style="{'font-family': 'PingFangSC-Semibold','backgroundColor': '#f5f7fa','color':'#5c6065'}" fit highlight-current-row class="c-width100 color-222 family-PingFangSC-Regular font-s12">
			 	<el-table-column align="center" label="排序">
          <template slot-scope="scope">
            <span>{{scope.row.sort}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="名称">
          <template slot-scope="scope">
            <span>{{scope.row.name}}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="permission == 3" align="center" label="操作">
          <template slot-scope="scope">
            <b @click="deleteTag(scope.row.gtId)">删除</b><span class="color-409">|</span>
            <b @click="addTag(scope.$index)">编辑</b>
          </template>
        </el-table-column>
		   </el-table>
		</div>
    <div class="block text-c">
      <!--分页组件 current-page:当前页   -->
      <!--   page-sizes:每页几条数据（选项）   -->
      <!--   page-size:每页几条数据   -->
      <!--   total:总条数   -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listQuery.total">
        </el-pagination>
    </div>
		<addTag v-if="isShowAdd" :tagData="tagData" @hideAdd="hideAdd" @saveTag="saveTag"></addTag>
	</div>
</template>
<script>
import { getTagList, deleteTag } from '@/api/goods'
import addTag from '../components/addTag.vue'
import { mall_mallInfo } from '@/utils/permission'
export default {
  components: {
    addTag
  },
  data() {
    return {
      permission: 3,
      listQuery: {
        currentPage: 1, // 当前页
        total: null, // 数据总条数
        nameInput: null,
        page: 1
      },
      isShowAdd: false, // 是否显示增加分类组件
      isShowEdit: false,
      tableData: [],
      tagData: []
    }
  },
  created() {
    this.getModelPermission()
    this.getList()
  },
  methods: {
    getModelPermission() {
      this.permission = mall_mallInfo()
    },
    getList() {
      getTagList(this.listQuery).then(res => {
        this.tableData = res.data.data.list.data
        this.listQuery.total = res.data.data.list.total
      }).catch(err => {
        console.log(err)
      })
    },
    addTag(index) {
      if (index == -1) {
        this.tagData = { gtId: null, companyId: null, name: null, sort: 0 }
      } else {
        this.tagData = {
          gtId: this.tableData[index].gtId,
          name: this.tableData[index].name,
          sort: this.tableData[index].sort
        }
      }
      this.isShowAdd = true
    },
    hideAdd(msg) {
      this.isShowAdd = msg
    },
    saveTag() {
      this.getList()
    },
    hideEdit(msg) {
      this.isShowEdit = msg
    },
    deleteTag(gtId) {
      this.$confirm('确定删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var data = { gtId: gtId }
        deleteTag(data).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        }).catch(err => {
          console.log(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSizeChange(val) { // 切换每页几条数据方法
      // console.log(`每页 ${val} 条`)
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) { // 切换当前页
      // console.log(`当前页: ${val}`)
      this.listQuery.page = val
      this.getList()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    linkToProduct(sortName) {
      this.$router.push({ path: `/mall/product/${sortName}` })
    }
  }
}
</script>

<style scoped>

</style>
